package com.cxyzy.basic_libs.anim

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.view.View
import android.view.animation.Animation
import android.view.animation.ScaleAnimation
import android.widget.RelativeLayout

/**
 * 圆环放大缩小动画
 */
class CircleAnim(context: Context, attributeSet: AttributeSet? = null) :
    RelativeLayout(context, attributeSet) {

    init {
        addView(context, 300, "#009ad6")
        addView(context, 250, "#fab27b")
        addView(context, 200, "#f15b6c")
        addView(context, 150, "#6950a1")
        addView(context, 100, "#45b97c")
        scale(this)
    }

    private fun addView(context: Context, size: Int, colorStr: String): View {
        val view = View(context)
        view.background = getCircle(colorStr)
        val layoutParams = LayoutParams(size, size)
        layoutParams.addRule(CENTER_IN_PARENT)
        addView(view, layoutParams)
        return view
    }

    /**
     * 动画： 先放大，再缩小，再放大，一直循环
     */
    private fun scale(srcView: View) {
        val animateTime = 2000L

        val scaleDownAnimation = getScaleDownAnimation(animateTime)

        val scaleUpAnimation = getScaleUpAnimation(animateTime)

        scaleDownAnimation.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationEnd(animation: Animation) {
                srcView.startAnimation(scaleUpAnimation)
            }

            override fun onAnimationStart(animation: Animation) {}
            override fun onAnimationRepeat(animation: Animation) {}
        })

        scaleUpAnimation.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationEnd(animation: Animation) {
                srcView.startAnimation(scaleDownAnimation)
            }

            override fun onAnimationStart(animation: Animation) {}
            override fun onAnimationRepeat(animation: Animation) {}
        })

        srcView.startAnimation(scaleDownAnimation)
    }

    /**
     * 放大动画
     */
    private fun getScaleDownAnimation(animateTime: Long): ScaleAnimation {
        val scaleDownAnimation = ScaleAnimation(
            1f, 2f, 1f, 2f,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f
        )
        scaleDownAnimation.duration = animateTime
        return scaleDownAnimation
    }

    /**
     * 缩小动画
     */
    private fun getScaleUpAnimation(animateTime: Long): ScaleAnimation {
        val scaleUpAnimation = ScaleAnimation(
            2f, 1f, 2f, 1f,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f
        )
        scaleUpAnimation.duration = animateTime
        return scaleUpAnimation
    }

    /**
     * 获取空心圆
     */
    private fun getCircle(strokeColorStr: String) = GradientDrawable().also {
        it.shape = GradientDrawable.OVAL
        val strokeWidth = 2 // 边框宽度
        val strokeColor = Color.parseColor(strokeColorStr) //边框颜色
        it.setStroke(strokeWidth, strokeColor)
    }
}